package com.example.demo.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Role;
import com.example.demo.entity.UserRole;
import com.example.demo.vo.Result;
import com.example.demo.exception.ApplicationException;
import com.example.demo.service.UserRoleService;
import com.example.demo.mapper.UserRoleMapper;
import com.example.demo.vo.ResultCode;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;

/**
* @author 15085202591
* @description 针对表【user_role(用户角色关联)】的数据库操作Service实现
* @createDate 2024-06-07 14:52:04
*/
@Slf4j
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService{

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Long> getRoleIdByUserId(Long UserId) {
        List<UserRole> user_id = userRoleMapper.selectList(new QueryWrapper<UserRole>().eq("user_id", UserId));
        List<Long> list = new ArrayList<>();
        for (UserRole userRole : user_id) {
            Long roleId = userRole.getRoleId();
            list.add(roleId);
        }
        return list;
    }

    @Override
    public void addUserRole(UserRole userRole) {
        int row = userRoleMapper.insert(userRole);
        if (row != 1) {
            log.warn("服务错误");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A9999));
        }

        log.info("新增成功!");
    }
}




